package com.ww.prodiver.service.impl;

import com.ww.prodiver.util.MongoUtil;
import com.ww.service.UserService;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName:
 * @author:
 * @Description:
 * @Date
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    MongoTemplate mongoTemplate;

    public void add() {
        User user1 = new User();
        user1.setUserId("22");
        user1.setUsername("dinglei");
        MongoUtil.insert(user1);
    }

    public List<User> findAllUser() {
        List<User> users = mongoTemplate.findAll(User.class);
        return users;
    }


    @Override
    public User findUserByUserId(String userId) {
        Query query = new Query().addCriteria(Criteria.where("userId").is(userId));
        return mongoTemplate.findOne(query, User.class);
    }


}
